package com.tg.app.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.view.View;

import com.appbase.custom.constant.CommonConstants;
import com.base.BaseActivity;
import com.tg.app.R;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;


/**
 * Describe:
 * Created by Rance Rang on 2019-06-04.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public abstract class DeviceSettingsBaseActivity extends BaseActivity {
    public  Handler mHandler=new Handler();
    public Boolean mIsSetSuccess=false;


    public void startSendui(){
        mIsSetSuccess=false;
        mHandler.postDelayed(timeRunable,15000);
    }

    public void completeSend(){
        mIsSetSuccess=true;
        mHandler.removeCallbacks(timeRunable);
    }

    public Runnable timeRunable= () -> {
        if (!mIsSetSuccess){
            onTimeFinish();
        }
    };

    protected void onTimeFinish() {
        showToast(R.string.setting_fail);
        onSetupFailed();
    }

    protected void onSetupFailed(){}

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public  void sendUpdateSettingBroadcast(DeviceSettingsInfo info){
        sendUpdateSettingBroadcast(info, null);
    }

    public void sendUpdateSettingBroadcast(DeviceSettingsInfo info, DeviceItem deviceItem){
        Intent intent = new Intent();
        intent.setAction(DeviceSettingsActivity.ACTION_DEVICE_SETTINGS);
        intent.putExtra(DeviceSettingsActivity.EXT_DEVICE_INFO,info);
        if (deviceItem != null){
            intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
        }
        sendBroadcast(intent);
    }

    public void backClickEvent(){
        if (findViewById(R.id.back_toolbar) != null){
            findViewById(R.id.back_toolbar).setOnClickListener(v -> {
                mHandler.removeCallbacks(timeRunable);
                if (onBackClick()){
                    finish();
                }

            });
        }
    }

    @Override
    public void onBackPressed() {
        mHandler.removeCallbacks(timeRunable);
        if (onBackClick()){
            finish();
        }
    }
    protected boolean onBackClick(){

        return  true;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}
